package com.video.web.controller.video;

import com.video.common.core.domain.entity.SysUser;
import com.video.common.exception.ServiceException;
import com.video.system.service.ISysUserService;
import com.video.video.domain.DUser;
import com.video.video.domain.dto.DWthdrawalRecordDto;
import com.video.video.domain.vo.DWthdrawalRecordQuery;
import com.video.video.service.IDUserService;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.video.common.annotation.Log;
import com.video.common.core.controller.BaseController;
import com.video.common.core.domain.AjaxResult;
import com.video.common.enums.BusinessType;
import com.video.video.domain.DWthdrawalRecord;
import com.video.video.service.IDWthdrawalRecordService;
import com.video.common.utils.poi.ExcelUtil;
import com.video.common.core.page.TableDataInfo;

/**
 * 提现记录Controller
 *
 * @author ruoyi
 * @date 2022-09-27
 */
@RestController
@RequestMapping("/video/DWthdrawalRecord")
public class DWthdrawalRecordController extends BaseController
{
    @Autowired
    private IDWthdrawalRecordService dWthdrawalRecordService;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private IDUserService idUserService;

    /**
     * 查询提现记录列表
     */
    @PreAuthorize("@ss.hasPermi('video:DWthdrawalRecord:list')")
    @GetMapping("/list")
    public TableDataInfo list(DWthdrawalRecordQuery query)
    {
        startPage();
        List<DWthdrawalRecordDto> list = dWthdrawalRecordService.selectDWthdrawalRecordList(query);
        return getDataTable(list);
    }

    /**
     * 导出提现记录列表
     */
    @PreAuthorize("@ss.hasPermi('video:DWthdrawalRecord:export')")
    @Log(title = "提现记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DWthdrawalRecordQuery query)
    {
        List<DWthdrawalRecordDto> list = dWthdrawalRecordService.selectDWthdrawalRecordList(query);
        ExcelUtil<DWthdrawalRecordDto> util = new ExcelUtil<DWthdrawalRecordDto>(DWthdrawalRecordDto.class);
        util.exportExcel(response, list, "提现记录数据");
    }

    /**
     * 获取提现记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('video:DWthdrawalRecord:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(dWthdrawalRecordService.selectDWthdrawalRecordById(id));
    }

    /**
     * 新增提现记录
     */
    @PreAuthorize("@ss.hasPermi('video:DWthdrawalRecord:add')")
    @Log(title = "提现记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DWthdrawalRecord dWthdrawalRecord)
    {
        //判断会员是否存在
        DUser huiyuan= idUserService.selectDUserById(dWthdrawalRecord.getUid());
        if(huiyuan == null){
            throw new ServiceException("会员不存在");
        }
        if(huiyuan.getLevel().equals("5") ){
            throw new ServiceException("合伙人不能进行提现");
        }

        if(huiyuan.getCommissionBalance().compareTo(dWthdrawalRecord.getAmount()) ==-1 ){
            throw new ServiceException("提现金额大于返佣余额");
        }
        return toAjax(dWthdrawalRecordService.insertDWthdrawalRecord(dWthdrawalRecord));
    }

    /**
     * 修改提现记录（审核）
     */
    @PreAuthorize("@ss.hasPermi('video:DWthdrawalRecord:edit')")
    @Log(title = "提现记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DWthdrawalRecord dWthdrawalRecord)
    {
        Long userId = getLoginUser().getUserId();
        SysUser user=userService.selectUserById(userId);
        if(user == null){
            throw new ServiceException("会话过期,请重新登录");
        }

        DWthdrawalRecord record= dWthdrawalRecordService.selectDWthdrawalRecordById(dWthdrawalRecord.getId());
        if(record == null){
            throw new ServiceException("提现申请记录不存在");
        }

        if(!record.getAudiStatus().equals("1")){
            throw new ServiceException("改提现记录已审核,请刷新页面");
        }

        //判断会员是否存在
        DUser huiyuan= idUserService.selectDUserById(dWthdrawalRecord.getUid());
        if(huiyuan == null){
            throw new ServiceException("会员不存在");
        }
        if(huiyuan.getLevel().equals("5") ){
            throw new ServiceException("合伙人不能进行提现");
        }

        if(huiyuan.getCommissionBalance().compareTo(dWthdrawalRecord.getAmount()) ==-1 ){
            throw new ServiceException("提现金额大于返佣余额");
        }
        dWthdrawalRecord.setUpdateBy(String.valueOf(userId));
        return toAjax(dWthdrawalRecordService.updateDWthdrawalRecord(dWthdrawalRecord));
    }

    /**
     * 删除提现记录
     */
    @PreAuthorize("@ss.hasPermi('video:DWthdrawalRecord:remove')")
    @Log(title = "提现记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(dWthdrawalRecordService.deleteDWthdrawalRecordByIds(ids));
    }
}
